#region Summary

// 
// Owner : JackieHan 
// 
// Solution : ResearchTest 
// 
// Project : JackieHan.ConfOrm.Test
// 
// File : TestOneToOne.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 测试一对一关系的配置
// 
// History : 
//			2011-06-28 17:00:20	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using ConfOrm;
using ConfOrm.NH;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Tool.hbm2ddl;
using NUnit.Framework;

#endregion

namespace JackieHan.ConfOrm.Test.OneToOne
{
    /// <summary> 
    /// 测试一对一关系的配置
    /// </summary>
    [TestFixture]
    public class TestOneToOne
    {

        [SetUp]
        public void SetUp()
        {
        }

        [Test]
        public void TestJustConf()
        {
            var mapping = GetMapping();
            Console.WriteLine(NhConfig.Serializer(mapping));
        }

        public HbmMapping GetMapping()
        {
            var orm = new ObjectRelationalMapper();
            var mapper = new Mapper(orm);
            var entityTypes = new[] { typeof(Person), typeof(Address) };
            orm.TablePerClass(entityTypes);

            orm.OneToOne<Person, Address>();

            return mapper.CompileMappingFor(entityTypes);
        }

        [Test]
        public void Test()
        {
            var config = NhConfig.ConfigureNhiberate();

            config.AddDeserializedMapping(GetMapping(), "OneToOne");

            SchemaMetadataUpdater.QuoteTableAndColumns(config);

            new SchemaExport(config).Drop(false, true);

            new SchemaExport(config).Create(true, true);

            var factory = config.BuildSessionFactory();

            using (var session = factory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                var address = new Address { Street = "HuLiDaDao", CivicNumber = 123 };
                var person = new Person { Name = "JackieHan", Address = address };
                address.Person = person;
                session.Save(person);
                tx.Commit();
            }

            //new SchemaExport(config).Drop(false, true);
        }

        [TearDown]
        public void TearDown()
        {
        }

    }
}